package com.feijia.aijingjingus;


import android.content.Context;
import android.graphics.Color;
import android.text.style.ForegroundColorSpan;

import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.DayViewDecorator;
import com.prolificinteractive.materialcalendarview.DayViewFacade;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import static com.feijia.aijingjingus.GlobalHelper.FirstDay;
import static com.feijia.aijingjingus.GlobalHelper.Menstruation;
import static com.feijia.aijingjingus.GlobalHelper.ONE_DAY_MS;
import static com.feijia.aijingjingus.GlobalHelper.PeriodLength;

/**
 * Created by 菲伽 on 2017/4/14.
 */

public class MCVPeriodDecorator implements DayViewDecorator {
    private final Context context;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    public MCVPeriodDecorator( Context context) {
        this.context =context;
    }
    /**
     * 设置效果
     * @param day {@linkplain CalendarDay} to possibly decorate
     *
     * @return
     */
    @Override
    public boolean shouldDecorate(CalendarDay day) {
        try {
            Date date = sdf.parse(FirstDay);
            Calendar cl = Calendar.getInstance();
            cl.setTime(date);
            long firstDay =(cl.getTimeInMillis()/ ONE_DAY_MS)%Menstruation;
            cl.add(Calendar.DAY_OF_MONTH,PeriodLength);
            long lastDay = (cl.getTimeInMillis()/ ONE_DAY_MS)%Menstruation;
            long nowDay = (day.getCalendar().getTimeInMillis()/ ONE_DAY_MS)%Menstruation;
            return (nowDay>=firstDay&&nowDay<lastDay);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 具体实现的效果
     * @param view View to decorate
     */
    @Override
    public void decorate(DayViewFacade view) {
        //view.addSpan(new DotSpan(5, color));
        view.addSpan(new ForegroundColorSpan(Color.WHITE));
        view.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.calendarview_red_big));

    }
}
